package cn.iocoder.yudao.module.cdk.controller.admin.cdkcdkuser;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.ratelimiter.core.annotation.RateLimiter;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkcdkuser.vo.CdkCdkuserPageReqVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkcdkuser.vo.CdkCdkuserRespVO;
import cn.iocoder.yudao.module.cdk.controller.admin.cdkcdkuser.vo.CdkCdkuserSaveReqVO;
import cn.iocoder.yudao.module.cdk.dal.dataobject.cdkcdkuser.CdkCdkuserDO;
import cn.iocoder.yudao.module.cdk.service.cdkcdkuser.CdkCdkuserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - cdk和用户的绑定关系")
@RestController
@RequestMapping("/cdk/cdkuser")
@Validated
public class CdkCdkuserController {

    @Resource
    private CdkCdkuserService cdkuserService;

    @RateLimiter(count = 5, timeUnit = TimeUnit.MINUTES)
    @PostMapping("/create")
    @Operation(summary = "创建cdk和用户的绑定关系")
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:create')")
    public CommonResult<Integer> createCdkuser(@Valid @RequestBody CdkCdkuserSaveReqVO createReqVO) {
        return success(cdkuserService.createCdkuser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新cdk和用户的绑定关系")
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:update')")
    public CommonResult<Boolean> updateCdkuser(@Valid @RequestBody CdkCdkuserSaveReqVO updateReqVO) {
        cdkuserService.updateCdkuser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除cdk和用户的绑定关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:delete')")
    public CommonResult<Boolean> deleteCdkuser(@RequestParam("id") Integer id) {
        cdkuserService.deleteCdkuser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得cdk和用户的绑定关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:query')")
    public CommonResult<CdkCdkuserRespVO> getCdkuser(@RequestParam("id") Integer id) {
        CdkCdkuserDO cdkuser = cdkuserService.getCdkuser(id);
        return success(BeanUtils.toBean(cdkuser, CdkCdkuserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得cdk和用户的绑定关系分页")
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:query')")
    public CommonResult<PageResult<CdkCdkuserRespVO>> getCdkuserPage(@Valid CdkCdkuserPageReqVO pageReqVO) {
        PageResult<CdkCdkuserDO> pageResult = cdkuserService.getCdkuserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CdkCdkuserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出cdk和用户的绑定关系 Excel")
    @PreAuthorize("@ss.hasPermission('cdk:cdkuser:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCdkuserExcel(@Valid CdkCdkuserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CdkCdkuserDO> list = cdkuserService.getCdkuserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "cdk和用户的绑定关系.xls", "数据", CdkCdkuserRespVO.class,
                        BeanUtils.toBean(list, CdkCdkuserRespVO.class));
    }

}